#ifndef __MM_GFP_H__
#define __MM_GFP_H__


/*
 * GFP bitmasks..
 */
/* Zone modifiers in GFP_ZONEMASK (see linux/mmzone.h - low two bits) */
#define __GFP_DMA       0x01
#define __GFP_HIGHMEM   0x02

/*
 * Action modifiers - doesn't change the zoning
 *
 * __GFP_REPEAT: Try hard to allocate the memory, but the allocation attempt
 * _might_ fail.  This depends upon the particular VM implementation.
 *
 * __GFP_NOFAIL: The VM implementation _must_ retry infinitely: the caller
 * cannot handle allocation failures.
 *
 * __GFP_NORETRY: The VM implementation must not retry indefinitely.
 */
#define __GFP_WAIT      0x10    /* Can wait and reschedule? */
#define __GFP_HIGH      0x20    /* Should access emergency pools? */
#define __GFP_IO        0x40    /* Can start physical IO? */
#define __GFP_FS        0x80    /* Can call down to low-level FS? */
#define __GFP_COLD      0x100   /* Cache-cold page required */
#define __GFP_NOWARN    0x200   /* Suppress page allocation failure warning */
#define __GFP_REPEAT    0x400   /* Retry the allocation.  Might fail */
#define __GFP_NOFAIL    0x800   /* Retry for ever.  Cannot fail */
#define __GFP_NORETRY   0x1000  /* Do not retry.  Might fail */
#define __GFP_NO_GROW   0x2000  /* Slab internal usage */
#define __GFP_COMP      0x4000  /* Add compound page metadata */
#define __GFP_ZERO      0x8000  /* Return zeroed page on success */

#define __GFP_BITS_SHIFT 16     /* Room for 16 __GFP_FOO bits */
#define __GFP_BITS_MASK ((1 << __GFP_BITS_SHIFT) - 1)

/* if you forget to add the bitmask here kernel will crash, period */
#define GFP_LEVEL_MASK (__GFP_WAIT|__GFP_HIGH|__GFP_IO|__GFP_FS| \
                        __GFP_COLD|__GFP_NOWARN|__GFP_REPEAT| \
                        __GFP_NOFAIL|__GFP_NORETRY|__GFP_NO_GROW|__GFP_COMP)

#define GFP_ATOMIC      (__GFP_HIGH)
#define GFP_NOIO        (__GFP_WAIT)
#define GFP_NOFS        (__GFP_WAIT | __GFP_IO)
#define GFP_KERNEL      (__GFP_WAIT | __GFP_IO | __GFP_FS)
#define GFP_USER        (__GFP_WAIT | __GFP_IO | __GFP_FS)
#define GFP_HIGHUSER    (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HIGHMEM)

/* Flag - indicates that the buffer will be suitable for DMA.  Ignored on some
   platforms, used as appropriate on others */

#define GFP_DMA         __GFP_DMA

static inline void arch_free_page(struct page* page, int order) { }

extern struct page * __alloc_pages(u32 , u32, struct zonelist *);

static inline struct page* alloc_pages_node(int nid, u32 gfp_mask, u32 order) {
  if (unlikely(order >= MAX_ORDER)) {
    return NULL;
  }

  return __alloc_pages(gfp_mask, order,
                       NODE_DATA(nid)->node_zonelists + (gfp_mask & GFP_ZONEMASK));
}

#endif  // __MM_GFP_H__
